<?php

namespace App\Http\Controllers\Admins\Users;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\Users;
use Illuminate\Http\Request;
use App\Models\OperationalLogs;
use Illuminate\Support\Facades\DB;

class UsersController extends BaseAdminController
{

    public function index(Request $request){
        $condition = $request->get('condition','title');
        if($request->isMethod('post')){
            # 要执行的代码
//            $roles = array('center','is_super_user');
            $query = Users::query()->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
//            $is_leader = $request->get('is_leader','all');
//            if($is_leader != 'all'){
//                $query->where('is_leader', $is_leader);
//            }
//            $department_id = $request->get('department_id','all');
//            if($department_id != 'all'){
//                $query->where('department_id', $department_id);
//            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $result[] = Users::getDataByID($val['id']);
                }
            }
            return ret([
                'total'=>$total,
                'data'=>$result
            ]);
        }
        $admin_id = $this->admin_id;
        $subtitle = '用户管理';
        return view('v1.admin.users.index',compact('subtitle','admin_id'));
    }

    public function show(Request $request,$id){
        $model = Users::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }
        $subtitle = '查看用户';
        $view = 'v1.admin.users.show';
        $data = Users::getDataByID($id);
        $compact = compact('subtitle','data');
        return view($view,$compact);
    }

    public function changeStatus(Request $request,$id)
    {
        $model = Users::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }

        if(!empty( $_POST['status'])){
            $status_update = $_POST['status'];
        }else{
            $status_update = 1;
        }
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['users_manage'],
            'operate_item'=>Users::query()->where($where)->value('id'),
            'operate_content'=>OperationalLogs::$operate['change_status'].'为：'.$status_update,
        ];
        DB::beginTransaction();
        try {
            Users::query()->where($where)->update([
                'is_alumni'=>$status_update,
            ]);
            #添加操作日志
            OperationalLogs::AddOperationalLogs($logs);
            DB::commit();
            return AdminRet('操作成功!');
        } catch (\Exception $exception) {
            DB::rollBack();
            return AdminRetError($exception->getMessage());
        }

    }
}
